// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// This file is part of CodingEditor.
// Note:	This project is derived from Peter Project
//			(hosted on sourceforge and codeplex)
//
// Copyright (c) 2008-2009, CE Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


namespace CE.CustomControls
{
	using System.Drawing;
	using System.Windows.Forms;

	public class GListBox : ListBox
	{
		#region Fields

		private ImageList _myImageList;

		#endregion Fields

		#region Constructors

		public GListBox()
		{
			// Set owner draw mode
			this.DrawMode = DrawMode.OwnerDrawFixed;
			this.BorderStyle = BorderStyle.FixedSingle;
		}

		#endregion Constructors

		#region Properties

		public ImageList ImageList
		{
			get { return _myImageList; }
			set { _myImageList = value; }
		}

		#endregion Properties

		#region Methods

		protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
		{
			e.DrawBackground();
			e.DrawFocusRectangle();
			if (this.Items.Count > 0) {
				GListBoxItem item;
				Rectangle bounds = e.Bounds;
				Size imageSize = _myImageList.ImageSize;
				try {
					item = (GListBoxItem)Items[e.Index];
					if (item.ImageIndex != -1) {
						ImageList.Draw(e.Graphics, bounds.Left, bounds.Top, item.ImageIndex);
						e.Graphics.DrawString(item.Text, e.Font, new SolidBrush(e.ForeColor),
						                      bounds.Left + imageSize.Width, bounds.Top);
					} else {
						e.Graphics.DrawString(item.Text, e.Font, new SolidBrush(e.ForeColor),
						                      bounds.Left, bounds.Top);
					}
				}
				catch{
					if (e.Index != -1) {
						e.Graphics.DrawString(Items[e.Index].ToString(), e.Font,
						                      new SolidBrush(e.ForeColor), bounds.Left, bounds.Top);
					} else {
						e.Graphics.DrawString(Text, e.Font, new SolidBrush(e.ForeColor),
						                      bounds.Left, bounds.Top);
					}
				}
			}
			base.OnDrawItem(e);
		}

		#endregion Methods
	}

	public class GListBoxItem
	{
		#region Fields

		private int _myImageIndex;
		private string _myText;

		#endregion Fields

		#region Constructors

		//constructor
		public GListBoxItem(string text, int index)
		{
			_myText = text;
			_myImageIndex = index;
		}

		public GListBoxItem(string text)
			: this(text, -1)
		{
		}

		public GListBoxItem()
			: this("")
		{
		}

		#endregion Constructors

		#region Properties

		public int ImageIndex
		{
			get { return _myImageIndex; }
			set { _myImageIndex = value; }
		}

		// properties
		public string Text
		{
			get { return _myText; }
			set { _myText = value; }
		}

		#endregion Properties

		#region Methods

		public override string ToString()
		{
			return _myText;
		}

		#endregion Methods
	}
}